const formidable = require('formidable')
const path = require('path')
const { Article} = require('../../model/article')

module.exports = (req,res,next) => {
    const form = formidable({
        uploadDir: path.join(__dirname,'../','../','public','upload'),
        keepExtensions:true
    });

    form.parse(req, async (err, fields, files) => {

       let result = await Article.create({
            title: fields.title,
            author: fields.author,
            pubdate: fields.pubdate,
            content: fields.content,
            pic: path.join('../','static',files.pic.path.split('public')[1]) 
        })
        res.redirect('/admin/article')
      });
}